#!/usr/bin/bash
# This is the one-liner installation script for docker. To install, run one of these two commands:
#
#     $ wget -qO- "gitee.com/liuyvjin/custom-ohmyzsh/raw/master/my_scripts/install_docker.sh" | bash
# or
#     $ curl -sL "gitee.com/liuyvjin/custom-ohmyzsh/raw/master/my_scripts/install_docker.sh" | bash
#
# Author: liujin

# -- 使用官方脚本安装 阿里云有点慢
# curl -fsSL get.docker.com -o get-docker.sh
# sudo sh get-docker.sh --mirror Aliyun
# rm get-docker.sh

# -- 使用清华源安装
sudo apt-get remove docker docker-engine docker.io  # 卸载旧版本
sudo apt-get install apt-transport-https ca-certificates curl gnupg lsb-release  # 使用 apt 安装一些必须工具

# 添加 gpg 和清华源
sudo mkdir /etc/apt/keyrings/
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /etc/apt/keyrings/docker.gpg
echo \
  "deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.gpg] https://mirrors.tuna.tsinghua.edu.cn/docker-ce/linux/ubuntu \
  $(lsb_release -cs) stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null

# 安装 docker
sudo apt-get update
sudo apt-get install docker-ce docker-ce-cli containerd.io

# -- 启动和配置
# 启动 Docker
sudo systemctl enable docker
sudo systemctl start docker

# 建立 Docker 用户组
sudo groupadd docker 2>/dev/null
sudo usermod -aG docker $USER

echo ">>> docker 将在注销并重新登录后生效"
echo ">>> 你可以在之后运行以下命令测试 docker 是否安装正确:\n\n        docker run --rm hello-world\n"